// pages/submit/submit.js
const app = getApp()
var wh = require("../../utils/wh.js");
Page({

  /**
   * 页面的初始数据 
   */
  data: {
    num: 1,
    modal: false,
    istrue: true,
    group_id: '',
    istype: 0, //判断是什么什么商品（拼团1，砍价2，秒杀3,普通4）
    cart_id: '',
    user_bar_id: '',
    user_sec_id: '',
    details: '',
    ShopDetail: '',
    coupon_id: 0,
    amount: '',
    discount: '请选择优惠券',
    express_type: 2, //物流或到店自取
    is_submit: 0,
    order_id: '',
    addresst: '',
    group_id: '',
    group_number:'' ,
    sponsor_id:''
  },
  // 提交订单
  formSubmit(e) {
    if (!this.data.details.address && this.data.express_type==1){
      wx.showToast({
        title: '没有收货地址',
        icon: 'none'
      });
      return;
    }
    if (this.data.is_submit == 1) {
      wx.showToast({
        title: '请勿重复提交',
        icon: 'none'
      });
      return;
    }
    this.data.is_submit = 1;
    let that = this,
      istype = this.data.istype;
    let data = {
      express_type: that.data.express_type,
      cuopon_id: that.data.cuopon_id,
      remark: e.detail.value.remark,
      form_id: e.detail.formId,
    };
    if (that.data.express_type == 1){
      data.address_id=that.data.details.address.address_id
    }
    if (istype == 4) {
      data.cart_id = that.data.cart_id
    } else if (istype == 2) {
      data.user_bar_id = that.data.user_bar_id
    } else if (istype == 3){
      data.user_sec_id = that.data.user_sec_id
    } else if (istype == 1){
      data.user_gro_id = that.data.user_gro_id
    }
    wh.Post('/api/mall.order/submit', data, that.startPay)
  },
  // 开始支付及支付后的回调 
  startPay(req) {
    console.log('订单号', req)
    let that = this; 
    that.setData({
      order_id: req.order_id
    })
    wh.Post('/api/mall.order/payment', {
      order_id: req.order_id
    }, function(res) {
      console.log('支付信息', res)
      // 微信支付
      wx.requestPayment({
        'timeStamp': res.timeStamp + '',
        'nonceStr': res.nonceStr,
        'package': res.package,
        'signType': res.signType,
        'paySign': res.paySign,
        'success': function(ref) {
          console.log('支付成功', ref)
          wx.showToast({
            title: '支付成功',
            icon: 'success',
            duration: 2000,
            complete: function() {
              if (that.data.istype == 1){
                wx.redirectTo({
                  url: '../joinGroup/joinGroup?sponsor_id=' + that.data.sponsor_id + '&group_id=' + that.data.group_id + '&group_number=' + that.data.group_number,
                })
              }else{
              wx.reLaunch({
                url: '../paysuss/paysuss',
              })
              }
            }
          })
        },
        'fail': function(ref) {
          if (ref.errMsg == 'requestPayment:fail cancel') {
            wx.navigateTo({
              url: '../orderDetail/ordType?order_id=' + that.data.order_id + '&idx=1',
            })
          } else {
            wx.showToast({
              title: '支付失败',
              icon: 'fail',
              duration: 2000
            })
          }
        }
      })
    })
  },
  // 跳转至商品详情
  toGoods(e) {
    let that = this,
      id = e.currentTarget.dataset.id,
      act_type = e.currentTarget.dataset.act_type;
    if (act_type == 1) {
      wx.navigateTo({
        url: '../details/details?goods_id=' + id,
      })
    } else if (act_type == 2) {
      wx.navigateTo({
        url: '../cutDetails/cutDetails?bargainId=' + id,
      })
    } else if (act_type == 3) {
      wx.navigateTo({
        url: '../killDetails/killDetails?seckill_id=' + id,
      })
    } else if (act_type == 4) {
      wx.navigateTo({
        url: '../groupDetails/groupDetails?group_id=' + id,
      })
    }
  }, 
  nav() {
    let istrue = this.data.istrue,
      express_type = this.data.express_type;
    if (istrue) {
      express_type = 2
    } else {
      express_type = 1
    }
    this.setData({
      istrue: !istrue,
      express_type: express_type
    })
  },
  close: function() {
    this.setData({
      modal: false
    })
  },

  mycoupon() {
    wx.navigateTo({
      url: '../mycoupon/usable?money=' + this.data.details.order.goods_amount,
    })
  },
  address() {
    wx.navigateTo({
      url: '../address/address?where=1',
    })
  },
  again() {
    wx.navigateTo({
      url: '../paysuss/paysuss',
    })
  },
  // 获取提交订单页面商品信息及支付信息
  order_buy_info(istype, id) {
    let that = this,
      data = {};
    if (istype == 4) {
      data = {
        cart_id: id
      }
    } else if (istype == 2) {
      data = {
        user_bar_id: id
      }
    } else if (istype == 3) {
      data = {
        user_sec_id: id
      }
    } else if (istype == 1){
      data = {
        user_gro_id: id
      }
    }
    wh.Post('/api/mall.order/buy_info', data, function(res) {
      that.getShopDetail(res.cart_list.shop.shop_id)
      console.log('信息', res)
      if (that.data.addresst_id){
        res.address = that.data.addresst
      }
      that.setData({
        details: res,
        amount: res.order.goods_amount
      })
    })
  },
  // 单个收货地址详情
  user_address_info() {
    if (this.data.addresst_id){
    var that = this;
    wh.Post('/api/shop.User_address/user_address_info', {
      id: that.data.addresst_id
    }, function (res) {
      console.log('单个收货地址详情', res)
      that.setData({
        addresst: res,
      })
    });
    }
  },
  // 获取商家店铺详情
  getShopDetail(shop_id) {
    var that = this;
    wh.Post('/api/mall.shop/detail', {
      shop_id: shop_id
    }, function(res) {
      console.log('店铺详情', res)
      that.setData({
        ShopDetail: res,
      })
    });
  },
  // 根据优惠券ID和订单总额计算优惠金额
  coupon_discount(coupon_id) {
    let that = this,
      amount = this.data.amount;
    wh.Post('/api/mall.coupon/discount', {
      coupon_id: coupon_id,
      amount: amount
    }, function(res) {
      console.log('使用优惠券后', res)
      that.setData({
        amount: res.price,
        discount: res.discount,
      })
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    if (options.istype == 4) {
      this.setData({
        cart_id: options.cart_id,
        istype: options.istype,
      })
    } else if (options.istype == 2) {
      this.setData({
        user_bar_id: options.user_bar_id, 
        istype: options.istype,
      })
    } else if (options.istype == 3) {
      this.setData({
        user_sec_id: options.user_sec_id,
        istype: options.istype,
      })
    } else if (options.istype == 1){
      console.log(options)
      this.setData({
        user_gro_id: options.user_gro_id,
        istype: options.istype,
        group_id: options.group_id,
        group_number: options.group_number,
        sponsor_id: options.sponsor_id,
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.user_address_info()
    // 优惠券
    if (this.data.coupon_id > 0) {
      this.coupon_discount(this.data.coupon_id)
    }
    if (this.data.istype == 4) {
      this.order_buy_info(this.data.istype, this.data.cart_id)
    } else if (this.data.istype == 2) {
      this.order_buy_info(this.data.istype, this.data.user_bar_id)
    } else if (this.data.istype == 3) {
      this.order_buy_info(this.data.istype, this.data.user_sec_id)
    } else if (this.data.istype == 1){
      this.order_buy_info(this.data.istype, this.data.user_gro_id)
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})